package xml;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Vector;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class XMLProcess {
	private static String fileLogin = "src/xml/Log.xml";
	private static String fileConfig = "src/xml/Config.xml";
	private static String fileEn = "src/xml/en-en.xml";
	private static String fileVi = "src/xml/vi-vn.xml";
	
	/**
	 * Load XML Document from file Name
	 * @param fileName
	 * @return XML document
	 */
	public static Document loadXMLDocument(String fileName){
		File f = new File(fileName);
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
	    DocumentBuilder builder;
	    Document document = null;
		try {
			builder = factory.newDocumentBuilder();
			document = builder.parse(f);			
		}// handle exception creating DocumentBuilder
		catch (ParserConfigurationException parserError) {
			System.err.println("Parser Configuration Error");
			parserError.printStackTrace();
		}// handle exception reading data from file
		catch (IOException fileException) {
			System.err.println("File IO Error");
			fileException.printStackTrace();
		}// handle exception parsing XML document
		catch (SAXException parseException) {
			System.err.println("Error Parsing Document");
			parseException.printStackTrace();
		}
		return document;
	}
	
	/** 
	 * Saves XML Document into XML file.
	 * @param fileName XML file name
	 * @param doc XML document to save
	 * @return true if method success false otherwise
	 */
	public static boolean saveXMLDocument(String fileName, Document doc) {
		// open output stream where XML Document will be saved
		File f = new File(fileName);
		FileOutputStream fos;
		Transformer transformer;
		try {
			fos = new FileOutputStream(f);
			// Use a Transformer for output
			TransformerFactory transformerFactory = TransformerFactory.newInstance();
			transformer = transformerFactory.newTransformer();
			DOMSource source = new DOMSource(doc);
			StreamResult result = new StreamResult(fos);			
			// transform source into result will do save
			transformer.transform(source, result);
		} catch (FileNotFoundException e) {
			System.out.println("Error occured: " + e.getMessage());
			return false;
		} catch (TransformerConfigurationException e) {
			System.out.println("Transformer configuration error: "+ e.getMessage());
			return false;
		} catch (TransformerException e) {
			System.out.println("Error transform: " + e.getMessage());
			return false;
		}
		return true;
	}

	public static Vector<String> readLogin(){		
		Vector<String> log = new Vector<String>();
		Document xml = loadXMLDocument(fileLogin);
		if(xml == null) return log = null;
		Node root = xml.getFirstChild();
		NodeList nodeList = root.getChildNodes();		
		for(int i=0; i<nodeList.getLength(); i++){
			if(nodeList.item(i).getNodeType() == Node.ELEMENT_NODE) {
				log.add(nodeList.item(i).getTextContent());
			}
		}
		return log;
	}
	
	public static void writeLogin(boolean rmb, String acc, String pwd){
		String flag;
		if(rmb) flag = "1";
		else flag = "0";
		Document xml = loadXMLDocument(fileLogin);
		NodeList remmeber = xml.getElementsByTagName("Remmeber");
		remmeber.item(0).setTextContent(flag);
		NodeList account = xml.getElementsByTagName("Account");
		account.item(0).setTextContent(acc);
		NodeList password = xml.getElementsByTagName("Pwd");
		password.item(0).setTextContent(pwd);
		saveXMLDocument(fileLogin, xml);
	}
	
	public static Vector<String> readConfig(){
		Vector<String> config = new Vector<String>();
		Document xml = loadXMLDocument(fileConfig);
		if(xml == null) return config = null;
		NodeList dft = xml.getElementsByTagName("Default");
		NodeList nodeList = dft.item(0).getChildNodes();
		for(int i=0; i<nodeList.getLength(); i++){
			if(nodeList.item(i).getNodeType() == Node.ELEMENT_NODE) {
				config.add(nodeList.item(i).getTextContent());
			}
		}
		return config;
	}
	
	public static void writeConfig(String server, String port){
		Document xml = loadXMLDocument(fileConfig);
		NodeList s = xml.getElementsByTagName("Server");
		s.item(1).setTextContent(server);
		NodeList p = xml.getElementsByTagName("Port");
		p.item(1).setTextContent(port);
		saveXMLDocument(fileConfig, xml);
	}
	
	public static Vector<String> readBtnLocale(int local){
		Vector<String> locale = new Vector<String>();
		Document xml = null;
		if(local == 0){
			xml = loadXMLDocument(fileVi);
		}else if(local == 1){
			xml = loadXMLDocument(fileEn);
		}		
		if(xml == null) return locale = null;
		NodeList btn = xml.getElementsByTagName("BUTTON");
		NodeList nodeList = btn.item(0).getChildNodes();
		for(int i=0; i<nodeList.getLength(); i++){
			if(nodeList.item(i).getNodeType() == Node.ELEMENT_NODE) {
				locale.add(nodeList.item(i).getTextContent());
			}
		}
		return locale;
	}
	
	public static Vector<String> readTitLocale(int local){
		Vector<String> locale = new Vector<String>();
		Document xml = null;
		if(local == 0){
			xml = loadXMLDocument(fileVi);
		}else if(local == 1){
			xml = loadXMLDocument(fileEn);
		}		
		if(xml == null) return locale = null;
		NodeList title = xml.getElementsByTagName("TITLE");
		NodeList nodeList = title.item(0).getChildNodes();
		for(int i=0; i<nodeList.getLength(); i++){
			if(nodeList.item(i).getNodeType() == Node.ELEMENT_NODE) {
				locale.add(nodeList.item(i).getTextContent());
			}
		}
		return locale;
	}
	
	public static Vector<String> readLblLocale(int local){
		Vector<String> locale = new Vector<String>();
		Document xml = null;
		if(local == 0){
			xml = loadXMLDocument(fileVi);
		}else if(local == 1){
			xml = loadXMLDocument(fileEn);
		}		
		if(xml == null) return locale = null;
		NodeList label = xml.getElementsByTagName("LABEL");
		NodeList nodeList = label.item(0).getChildNodes();
		for(int i=0; i<nodeList.getLength(); i++){
			if(nodeList.item(i).getNodeType() == Node.ELEMENT_NODE) {
				locale.add(nodeList.item(i).getTextContent());
			}
		}
		return locale;
	}
	
	public static Vector<String> readToolTipLocale(int local){
		Vector<String> locale = new Vector<String>();
		Document xml = null;
		if(local == 0){
			xml = loadXMLDocument(fileVi);
		}else if(local == 1){
			xml = loadXMLDocument(fileEn);
		}		
		if(xml == null) return locale = null;
		NodeList label = xml.getElementsByTagName("TOOLTIP");
		NodeList nodeList = label.item(0).getChildNodes();
		for(int i=0; i<nodeList.getLength(); i++){
			if(nodeList.item(i).getNodeType() == Node.ELEMENT_NODE) {
				locale.add(nodeList.item(i).getTextContent());
			}
		}
		return locale;
	}
	
	/**
	 * @category this about 50ns slower than [readMenuLocale_Fast] but get menu of FUNCTION
	 * @param local
	 * @return
	 */
	public static Vector<String> readMenuLocale(int local){
		Vector<String> locale = new Vector<String>();
		Document xml = null;
		if(local == 0){
			xml = loadXMLDocument(fileVi);
		}else if(local == 1){
			xml = loadXMLDocument(fileEn);
		}		
		if(xml == null) return locale = null;
		NodeList function = xml.getElementsByTagName("FUNCTION");
		NodeList funList = function.item(0).getChildNodes();
		for(int i=0; i<funList.getLength(); i++){
			if(funList.item(i).getNodeType() == Node.ELEMENT_NODE) {
				NodeList menuList = ((Element) funList.item(i)).getElementsByTagName("Menu");
				Node menu = menuList.item(0).getChildNodes().item(0);
				locale.add(menu.getTextContent());
			}
		}
		return locale;
	}	
	/**
	 * @category fastest but get all menu
	 * @param local
	 * @return
	 */
	public static Vector<String> readMenuLocale_Fast(int local){
		Vector<String> locale = new Vector<String>();
		Document xml = null;
		if(local == 0){
			xml = loadXMLDocument(fileVi);
		}else if(local == 1){
			xml = loadXMLDocument(fileEn);
		}		
		if(xml == null) return locale = null;
		NodeList menu = xml.getElementsByTagName("Menu");
		for(int i=0; i<menu.getLength(); i++){
			locale.add(menu.item(i).getTextContent());
		}
		return locale;
	}
	
	public static Vector<String> readSubMenuLocale(int local){
		Vector<String> locale = new Vector<String>();
		Document xml = null;
		if(local == 0){
			xml = loadXMLDocument(fileVi);
		}else if(local == 1){
			xml = loadXMLDocument(fileEn);
		}		
		if(xml == null) return locale = null;
		NodeList function = xml.getElementsByTagName("FUNCTION");
		NodeList funList = function.item(0).getChildNodes();
		for(int i=0; i<funList.getLength(); i++){
			if(funList.item(i).getNodeType() == Node.ELEMENT_NODE) {
				NodeList subMenuList = ((Element) funList.item(i)).getElementsByTagName("SubMenu");
				NodeList itemSubMenu = subMenuList.item(0).getChildNodes();
				for(int j=0; j<itemSubMenu.getLength(); j++){
					if(itemSubMenu.item(j).getNodeType() == Node.ELEMENT_NODE) {
						locale.add(itemSubMenu.item(j).getTextContent());
					}
				}
			}
		}
		return locale;
	}
	
	public static Vector<String> readSubMenuLocale_Fast(int local){
		Vector<String> locale = new Vector<String>();
		Document xml = null;
		if(local == 0){
			xml = loadXMLDocument(fileVi);
		}else if(local == 1){
			xml = loadXMLDocument(fileEn);
		}		
		if(xml == null) return locale = null;
		NodeList subMenuList = xml.getElementsByTagName("SubMenu");
		
		for(int i=0; i<subMenuList.getLength(); i++){
			NodeList itemSubMenu = subMenuList.item(i).getChildNodes();
			for(int j=0; j<itemSubMenu.getLength(); j++){
				if(itemSubMenu.item(j).getNodeType() == Node.ELEMENT_NODE) {
					locale.add(itemSubMenu.item(j).getTextContent());
				}
			}
		}
		return locale;
	}
	
	public static Vector<String> readTabLocale(int local){
		Vector<String> locale = new Vector<String>();
		Document xml = null;
		if(local == 0){
			xml = loadXMLDocument(fileVi);
		}else if(local == 1){
			xml = loadXMLDocument(fileEn);
		}		
		if(xml == null) return locale = null;
		NodeList function = xml.getElementsByTagName("FUNCTION");
		NodeList funList = function.item(0).getChildNodes();
		for(int i=0; i<funList.getLength(); i++){
			if(funList.item(i).getNodeType() == Node.ELEMENT_NODE) {
				NodeList subMenuList = ((Element) funList.item(i)).getElementsByTagName("Tab");
				NodeList itemSubMenu = subMenuList.item(0).getChildNodes();
				for(int j=0; j<itemSubMenu.getLength(); j++){
					if(itemSubMenu.item(j).getNodeType() == Node.ELEMENT_NODE) {
						locale.add(itemSubMenu.item(j).getTextContent());
					}
				}
			}
		}
		return locale;
	}
	
	public static Vector<String> readTabLocale_Fast(int local){
		Vector<String> locale = new Vector<String>();
		Document xml = null;
		if(local == 0){
			xml = loadXMLDocument(fileVi);
		}else if(local == 1){
			xml = loadXMLDocument(fileEn);
		}		
		if(xml == null) return locale = null;
		NodeList subMenuList = xml.getElementsByTagName("Tab");
		for(int i=0; i<subMenuList.getLength(); i++){
			NodeList itemSubMenu = subMenuList.item(i).getChildNodes();
			for(int j=0; j<itemSubMenu.getLength(); j++){
				if(itemSubMenu.item(j).getNodeType() == Node.ELEMENT_NODE) {
					locale.add(itemSubMenu.item(j).getTextContent());
				}
			}
		}
		return locale;
	}
	
	public static Vector<String> readExit(int local){
		Vector<String> locale = new Vector<String>();
		Document xml = null;
		if(local == 0){
			xml = loadXMLDocument(fileVi);
		}else if(local == 1){
			xml = loadXMLDocument(fileEn);
		}		
		if(xml == null) return locale = null;
		NodeList alert = xml.getElementsByTagName("EXIT");
		NodeList alertList = alert.item(0).getChildNodes();
		for(int i=0; i<alertList.getLength(); i++){
			if(alertList.item(i).getNodeType() == Node.ELEMENT_NODE) {
				locale.add(alertList.item(i).getTextContent());
			}
		}
		return locale;
	}
	
	public static Vector<String> readError(int local){
		Vector<String> locale = new Vector<String>();
		Document xml = null;
		if(local == 0){
			xml = loadXMLDocument(fileVi);
		}else if(local == 1){
			xml = loadXMLDocument(fileEn);
		}		
		if(xml == null) return locale = null;
		NodeList alert = xml.getElementsByTagName("ERROR");
		NodeList alertList = alert.item(0).getChildNodes();
		for(int i=0; i<alertList.getLength(); i++){
			if(alertList.item(i).getNodeType() == Node.ELEMENT_NODE) {
				locale.add(alertList.item(i).getTextContent());
			}
		}
		return locale;
	}
	
	public static Vector<String> readNotice(int local){
		Vector<String> locale = new Vector<String>();
		Document xml = null;
		if(local == 0){
			xml = loadXMLDocument(fileVi);
		}else if(local == 1){
			xml = loadXMLDocument(fileEn);
		}		
		if(xml == null) return locale = null;
		NodeList alert = xml.getElementsByTagName("NOTICE");
		NodeList alertList = alert.item(0).getChildNodes();
		for(int i=0; i<alertList.getLength(); i++){
			if(alertList.item(i).getNodeType() == Node.ELEMENT_NODE) {
				locale.add(alertList.item(i).getTextContent());
			}
		}
		return locale;
	}
	
	public static Vector<String> readSuccess(int local){
		Vector<String> locale = new Vector<String>();
		Document xml = null;
		if(local == 0){
			xml = loadXMLDocument(fileVi);
		}else if(local == 1){
			xml = loadXMLDocument(fileEn);
		}		
		if(xml == null) return locale = null;
		NodeList alert = xml.getElementsByTagName("SUCCESS");
		NodeList alertList = alert.item(0).getChildNodes();
		for(int i=0; i<alertList.getLength(); i++){
			if(alertList.item(i).getNodeType() == Node.ELEMENT_NODE) {
				locale.add(alertList.item(i).getTextContent());
			}
		}
		return locale;
	}
	
//	public static void main(String[] args){		
////		Vector<String> a = readLogin();
////		System.out.println(a.size());
////		for(int i =0; i<a.size();i++){
////			System.out.println(a.get(i));
////		}		
//////		writeLogin(true, "123", "456");
////		
////		Vector<String> b = readConfig();
////		System.out.println(b.size());
////		for(int i =0; i<b.size();i++){
////			System.out.println(b.get(i));
////		}
////		writeConfig("", "");
//		
////		Vector<String> c = readToolTipLocale(1);
////		for(int i =0; i<c.size();i++){
////			System.out.println(c.get(i));
////		}
//		Vector<String> d = readSubMenuLocale(0);
//		System.out.println(d.size());
//		for(int i =0; i<d.size();i++){
//			System.out.println(d.get(i));
//		}
//		
//		Vector<String>	e = readExit(0);
//		for(int i =0; i<e.size();i++){
//			System.out.println(e.get(i));
//		}
//		
//		
//	}
}
